import React from 'react';
import {
  Router as DefaultRouter,
  Route,
  Switch,
  StaticRouter,
} from 'react-router-dom';
import dynamic from 'umi/dynamic';
import renderRoutes from 'umi/lib/renderRoutes';
import history from '@@/history';
import RendererWrapper0 from 'C:/Users/xzz/Desktop/JYK/management/src/pages/.umi/LocaleWrapper.jsx';
import { routerRedux, dynamic as _dvaDynamic } from 'dva';

const Router = routerRedux.ConnectedRouter;

const routes = [
  {
    path: '/',
    component: __IS_BROWSER
      ? _dvaDynamic({
          component: () =>
            import(/* webpackChunkName: "layouts__BasicLayout" */ '../../layouts/BasicLayout'),
        })
      : require('../../layouts/BasicLayout').default,
    routes: [
      {
        path: '/collection',
        name: '收藏列表',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__collection__index" */ '../collection/index'),
            })
          : require('../collection/index').default,
        exact: true,
      },
      {
        path: '/share',
        name: '分享列表',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__share__index" */ '../share/index'),
            })
          : require('../share/index').default,
        exact: true,
      },
      {
        path: '/assetstandard/standardList',
        name: '标准与规范',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__assetstandard__standardList" */ '../assetstandard/standardList'),
            })
          : require('../assetstandard/standardList').default,
        exact: true,
      },
      {
        path: '/assetstandard/StandardMetaData',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__assetstandard__StandardMetaData" */ '../assetstandard/StandardMetaData'),
            })
          : require('../assetstandard/StandardMetaData').default,
        exact: true,
      },
      {
        path: '/assetstandard/StandardData',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__assetstandard__StandardData" */ '../assetstandard/StandardData'),
            })
          : require('../assetstandard/StandardData').default,
        exact: true,
      },
      {
        path: '/specialtopic',
        name: '专题管理',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__specialtopic__index" */ '../specialtopic/index'),
            })
          : require('../specialtopic/index').default,
        exact: true,
      },
      {
        path: '/article',
        name: '科研软件管理',
        routes: [
          {
            path: '/article/articleClassify',
            name: '分类管理',
            component: __IS_BROWSER
              ? _dvaDynamic({
                  component: () =>
                    import(/* webpackChunkName: "p__article__articleClassify" */ '../article/articleClassify'),
                })
              : require('../article/articleClassify').default,
            exact: true,
          },
          {
            path: '/article/index',
            name: '科研软件列表',
            component: __IS_BROWSER
              ? _dvaDynamic({
                  component: () =>
                    import(/* webpackChunkName: "p__article__index" */ '../article/index'),
                })
              : require('../article/index').default,
            exact: true,
          },
          {
            component: () =>
              React.createElement(
                require('C:/Users/xzz/Desktop/JYK/management/node_modules/_umi-build-dev@1.18.8@umi-build-dev/lib/plugins/404/NotFound.js')
                  .default,
                { pagesPath: 'src/pages', hasRoutesInConfig: true },
              ),
          },
        ],
      },
      {
        path: '/help',
        name: '帮助',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__help__index" */ '../help/index'),
            })
          : require('../help/index').default,
        exact: true,
      },
      {
        path: '/opinion',
        name: '意见',
        component: __IS_BROWSER
          ? _dvaDynamic({
              component: () =>
                import(/* webpackChunkName: "p__opinion__index" */ '../opinion/index'),
            })
          : require('../opinion/index').default,
        exact: true,
      },
      {
        component: () =>
          React.createElement(
            require('C:/Users/xzz/Desktop/JYK/management/node_modules/_umi-build-dev@1.18.8@umi-build-dev/lib/plugins/404/NotFound.js')
              .default,
            { pagesPath: 'src/pages', hasRoutesInConfig: true },
          ),
      },
    ],
  },
  {
    component: () =>
      React.createElement(
        require('C:/Users/xzz/Desktop/JYK/management/node_modules/_umi-build-dev@1.18.8@umi-build-dev/lib/plugins/404/NotFound.js')
          .default,
        { pagesPath: 'src/pages', hasRoutesInConfig: true },
      ),
  },
];
window.g_routes = routes;
const plugins = require('umi/_runtimePlugin');
plugins.applyForEach('patchRoutes', { initialValue: routes });

export { routes };

export default class RouterWrapper extends React.Component {
  unListen() {}

  constructor(props) {
    super(props);

    // route change handler
    function routeChangeHandler(location, action) {
      plugins.applyForEach('onRouteChange', {
        initialValue: {
          routes,
          location,
          action,
        },
      });
    }
    this.unListen = history.listen(routeChangeHandler);
    // dva 中 history.listen 会初始执行一次
    // 这里排除掉 dva 的场景，可以避免 onRouteChange 在启用 dva 后的初始加载时被多执行一次
    const isDva =
      history.listen
        .toString()
        .indexOf('callback(history.location, history.action)') > -1;
    if (!isDva) {
      routeChangeHandler(history.location);
    }
  }

  componentWillUnmount() {
    this.unListen();
  }

  render() {
    const props = this.props || {};
    return (
      <RendererWrapper0>
        <Router history={history}>{renderRoutes(routes, props)}</Router>
      </RendererWrapper0>
    );
  }
}
